﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.IService;
using STF.Blog.Common;
using STF.Blog.WebApp.Areas.ForumManage.Models.Fans;
using X.PagedList;
using STF.Blog.WebApp.Areas.ForumManage.Filters;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class FansController : Controller
    {
        private IFansService _ifs;
        public FansController(IFansService ifs)
        {
            _ifs = ifs;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search , int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _ifs.GetAllFansAsync();
            var dataList = new List<FansListViewModel>();

            foreach (var item in data) 
            {
                var model = new FansListViewModel
                {
                    Id = item.Id,
                    Mid = item.Mid,
                    Cid = item.Cid,
                    CreateTime = item.CreateTime,
                    UpdateTime = item.UpdateTime
                };
                dataList.Add(model);
            }

            ViewBag.Search = search;
            var pages = dataList.ToPagedList(page,PageConfig.PageSize);

            return View(pages);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            if (ModelState.IsValid) 
            {
                await _ifs.PutTrashAsync(id);
                return Redirect("~/ForumManage/Index");
            }
            return View();
        }
    }
}
